/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pope.jloading;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author pope945@gmail.com
 */
public class TaskTableModel extends AbstractTableModel{
    private List<TaskModel> values = new ArrayList<TaskModel>();
    
    public int getRowCount() {
        return values.size();
    }
    
    public int getColumnCount() {
        return TaskModel.TITLES.length;
    }
    
    public Object getValueAt(int rowIndex, int columnIndex) {
        TaskModel tm = values.get(rowIndex);
        return tm.getColumnValue(columnIndex);
    }
    
    @Override
    public String getColumnName(int column) {
        return TaskModel.TITLES[column];
    }
    
    // -------------------------------------------------- custom
    
    /**
     * 增加�?��新的下载任务
     * @param task
     */
    public void addTaskModel(TaskModel task) {
        this.values.add(task);
        int insertedRow = this.values.size() - 1; //edit 20080723
        fireTableRowsInserted(insertedRow,insertedRow);
    }
    
    /** 移除任务 */
    public void removeTasks(List<TaskModel> tasks) {
        this.values.removeAll(tasks);
       
       
    }
    
    /** 通过任务标识移除任务 */
    public void removeByTaskid(String taskid) {
        synchronized (values) {
            Iterator<TaskModel> it = values.iterator();
            while (it.hasNext()) {
                TaskModel tm = it.next();
                if (tm.getTaskid().equals(taskid)) {
                    it.remove();
                }
            }
        }
    }
    
    /**
     * 获得任务对象
     * @param rowIndex
     * @return
     */
    public TaskModel getTask(int rowIndex) {
        return values.get(rowIndex);
    }
    
}
